package com.nio.file.files;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class FilesFirstDemo {
    /**
     * 获取文件属性
     *
     * @throws IOException
     */
    @Test
    public void testGetLastModifiedTime() throws IOException, InterruptedException {
        File file = new File("I:\\jbProject\\javastudy\\src\\com\\nio\\file\\files\\FilesFirstDemo.java");
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSSXXX").format(file.lastModified()));
        BasicFileAttributes bAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
        //这里的时间没有加上时区  所以比上面的时间少8小时
        System.out.println(bAttributes.lastModifiedTime());
        Thread.sleep(3000);
        System.out.println(bAttributes.lastAccessTime());
    }

    @Test
    public void testCopy() throws IOException {

        String source = "I:\\jbProject\\javastudy\\src\\com\\nio\\file\\files\\source.txt";
        String target = "I:\\jbProject\\javastudy\\src\\com\\nio\\file\\files\\target.txt";
        Files.copy(Paths.get(source), Paths.get(target), StandardCopyOption.REPLACE_EXISTING);
    }


    @Test
    public void testCreateDirs() throws IOException {
        String filePath = getClass().getResource("/").getPath();
        filePath += File.separator + "tmp" + File.separator + "test.txt";

        if (Files.exists(Paths.get(filePath))) {

        }
        Path path1 = Files.createFile(Paths.get(filePath));
        Path path = Files.createDirectories(Paths.get(filePath));

    }

}
